########################################################################################
############### REPLICATION FILE - EXTREME DISTRICTS, MODERATE WINNERS #################
######################### JESSE M. CROSSON, SEPTEMBER 2019 #############################
########################################################################################

# Start writing to an output file

require("Matching")
require("rgenoud")
require("dplyr")
require("coefplot")
require("TeachingDemos")
require("stargazer")

sink("analysis-output.txt") 

#################================ TEST 1 ===============#####################

data1 <- read.csv("test1.csv")

abridged <- data.frame(cbind(data1$year, data1$treatment, data1$INCUMBENT, data1$OPEN, data1$IDEOLOGY, data1$moderate, data1$DIST_TO_ZERO, data1$party, 
                             data1$congress, data1$state_upper, data1$abs_closer_zero, data1$y2012, data1$y2010, data1$y2008, data1$post_reform, 
                             data1$calif, data1$wash, data1$np_score))

colnames(abridged) <- c("year", "treatment", "incumbent", "open", "ideology", "moderate", "candidate_ext", "party", "congress", 
                        "state_upper", "more_extreme", "y2012", "y2010", "y2008", "post_reform", "calif", "wash",
                        "np_score")

abridged$dist_extreme <- abs(data1$tw_district_ideo) # how extreme the district is
abridged$dist_ideo <- data1$tw_district_ideo
abridged$abs_np_score <- abs(data1$np_score)
#abridged$abs_dwd_score <- abs(data1$dw_dime)


##### Regression Models ##### 

model3 <- glm(moderate ~ treatment + party + incumbent + open + state_upper + dist_extreme + more_extreme, data=abridged, family='binomial')
summary(model3)

model3.FE <- glm(moderate ~ treatment + party + incumbent + open + state_upper + dist_extreme + more_extreme+ calif + wash, data=abridged, family='binomial')
summary(model3.FE) 

model3b <- glm(candidate_ext ~ treatment + party +incumbent + open + state_upper + dist_extreme + more_extreme , data=abridged)
summary(model3b)

model3b.FE <- glm(candidate_ext ~ treatment + party +incumbent + open + state_upper + dist_extreme + more_extreme + calif + wash, data=abridged)
summary(model3b.FE) 

model3.1 <- glm(candidate_ext ~ treatment + post_reform + incumbent + open + state_upper + party + dist_extreme + more_extreme, data=abridged)
summary(model3.1)

model3.1b <- glm(moderate ~ treatment + post_reform + incumbent + open + state_upper + party + dist_extreme + more_extreme, data=abridged, family="binomial")
summary(model3.1b)

# To create Table 1
stargazer(model3b, model3b.FE,  model3.1, model3, model3.FE,model3.1b, out = "table1.tex") 



## Marginal Effects Plot: Figure 3 ##

require("ggeffects")
require("ggplot2")
require(coefplot)

theme_safeskies <- theme_bw()+
  theme(
    plot.margin = unit(c(1, 1, 1, 1), units="line"),
    axis.title.y=element_blank(),
    axis.title.x = element_text(color="darkgray"),
    axis.text.y = element_text(color="#934f3f"),
    axis.text.y.right = element_text(color="#934f3f"),
    plot.title = element_text(size=14, hjust=-0.03, vjust=5, margin=margin(b=15, t=-4)),
    plot.subtitle = element_text(size=9, hjust=-0.05, vjust=6, margin=margin(b=15, t=-4)),
    panel.border = element_blank(),
    panel.grid.major.x = element_blank(),
    panel.grid.minor.x = element_blank(),
    panel.grid.major.y = element_line(size=.1, color="grey66"),
    panel.grid.minor.y = element_blank()
  )

predictdat_3bFE_calif <- ggpredict(model3b.FE, c("treatment"), condition = c(calif = 1))
predictdat_3bFE_wash <- ggpredict(model3b.FE, c("treatment"), condition= c(calif=0))
cali <- ecdf(subset(data1, calif == 1)$DIST_TO_ZERO)
cali(0.765) # 53th percentile
cali(0.642) # 39th percentile
wash <- ecdf(subset(data1, calif == 0)$DIST_TO_ZERO)
wash(0.897) # 58th percentile
wash(0.773) # 43rd percentile


p <- ggplot(subset(abridged, calif == 1), aes(candidate_ext))

pdf("figure3_calif.pdf")
p + geom_area(stat = "bin",fill = "#998EC3", alpha = .5, colour = "#998EC3", size = 1) +
  geom_vline(xintercept = .718, size = 1, linetype = "dashed", colour = "#999999") +
  geom_vline(xintercept = .618, size = 1, linetype = "dashed") +
  xlab("Folded CF-Score") +
  ggtitle("California") +
  theme_safeskies +
  theme(plot.title = element_text(face="bold", size = 25), axis.text=element_text(size=15), axis.title = element_text(size = 18.5),
        panel.background = element_rect(fill = "transparent", colour = NA), plot.background = element_rect(fill = "transparent", colour = NA)) 
dev.off()

p <- ggplot(subset(abridged, calif == 0), aes(candidate_ext))
pdf("figure3_wash.pdf")
p + geom_area(stat = "bin",fill = "#F1A340", alpha = .5, colour = "#F1A340", size = 1) +
  geom_vline(xintercept = 0.875, size = 1, linetype = "dashed", colour = "#999999") +
  geom_vline(xintercept = 0.776, size = 1, linetype = "dashed") +
  xlab("Folded CF-Score") +
  ggtitle("Washington") +
  theme_safeskies +
  theme(plot.title = element_text(face="bold", size = 25), axis.text=element_text(size=15), axis.title = element_text(size = 18.5),
        panel.background = element_rect(fill = "transparent", colour = NA), plot.background = element_rect(fill = "transparent", colour = NA)) 
dev.off()


##### Matching Analysis (Table 2) #####

match.data <- cbind(abridged$treatment, abridged$post_reform, abridged$moderate, abridged$incumbent, abridged$open, 
                    abridged$more_extreme, abridged$state_upper, abridged$dist_extreme, abridged$candidate_ext, 
                    abridged$dist_ideo, abridged$party, abridged$calif, abridged$wash)
match.data <- data.frame(na.omit(match.data))
names(match.data) <- c("treatment", "post_reform", "moderate", "incumbent", "open", "more_extreme", "state_upper", 
                       "dist_extreme", "cand_extreme", "dist_ideo", "party", "calif", "wash")
xcov1 <- cbind(match.data$incumbent, match.data$open, match.data$more_extreme, match.data$state_upper, match.data$dist_extreme)
colnames(xcov1) <- c("incumbent", "open", "more_extreme", "state_upper", "dist_extreme")
xcov1b <- cbind(match.data$incumbent, match.data$open, match.data$more_extreme, match.data$state_upper, match.data$dist_extreme, match.data$calif, match.data$wash)
colnames(xcov1b) <- c("incumbent", "open", "more_extreme", "state_upper", "dist_extreme", "calif", "wash")
xcov2 <- cbind(match.data$incumbent, match.data$post_reform, match.data$open, match.data$more_extreme, match.data$state_upper, match.data$dist_extreme)
colnames(xcov2) <- c("incumbent", "post_reform", "open", "more_extreme", "state_upper", "dist_extreme")
xcov2b <- cbind(match.data$incumbent, match.data$post_reform, match.data$open, match.data$more_extreme, match.data$state_upper, match.data$dist_extreme, match.data$calif, match.data$wash)
colnames(xcov2b) <- c("incumbent", "post_reform", "open", "more_extreme", "state_upper", "dist_extreme", "calif", "wash")
xcov3 <- cbind(match.data$incumbent, match.data$open, match.data$more_extreme, match.data$state_upper, match.data$dist_extreme, match.data$party)
colnames(xcov3) <- c("incumbent", "open", "more_extreme", "state_upper", "dist_extreme", "party")
xcov3b <- cbind(match.data$incumbent, match.data$open, match.data$more_extreme, match.data$state_upper, match.data$dist_extreme, match.data$party, match.data$calif, match.data$wash)
colnames(xcov3b) <- c("incumbent", "open", "more_extreme", "state_upper", "dist_extreme", "party", "calif", "wash")
xcov4 <- cbind(match.data$incumbent, match.data$post_reform, match.data$open, match.data$more_extreme, match.data$state_upper, match.data$dist_extreme, match.data$party)
colnames(xcov4) <- c("incumbent", "post_reform", "open", "more_extreme", "state_upper", "dist_extreme", "party")
xcov4b <- cbind(match.data$incumbent, match.data$post_reform, match.data$open, match.data$more_extreme, match.data$state_upper, match.data$dist_extreme, match.data$party, match.data$calif, match.data$wash)
colnames(xcov4b) <- c("incumbent", "post_reform", "open", "more_extreme", "state_upper", "dist_extreme", "party", "calif", "wash")

outcome1 <- match.data$moderate
outcome2 <- match.data$cand_extreme
tr1 <- match.data$treatment
tr2 <- match.data$post_reform


## ATT RESULTS (Table 2) ##

# outcome 2, treatment 1, and xcov3 # Model 1

set.seed(7312019)
matching <- Match(Y = outcome2, Tr = tr1, X=xcov3, ties=TRUE, estimand="ATT")
gen.matched <- GenMatch(Tr = tr1, X = xcov3, ties=TRUE)
set.seed(7312019)
gen.match1 <- Match(Y = outcome2, Tr = tr1, X=xcov3, Weight.matrix=gen.matched, estimand="ATT")
MatchBalance(tr1 ~ xcov3, match.out=gen.match1)

summary(gen.match1)

# outcome 2, treatment 1, and xcov3b # Model 2

set.seed(7312017)
matching <- Match(Y = outcome2, Tr = tr1, X=xcov3b, ties=TRUE, estimand="ATT")
gen.matched <- GenMatch(Tr = tr1, X = xcov3b, ties=TRUE)
set.seed(7312017)
gen.match2 <- Match(Y = outcome2, Tr = tr1, X=xcov3b, Weight.matrix=gen.matched, estimand="ATT")
MatchBalance(tr1 ~ xcov3, match.out=gen.match2)

summary(gen.match2) 

# outcome 2, treatment 1, and xcov4 # Model 3

set.seed(7312018)
matching <- Match(Y = outcome2, Tr = tr1, X=xcov4, ties=TRUE, estimand="ATT")
set.seed(7312018)
gen.matched <- GenMatch(Tr = tr1, X = xcov4, ties=TRUE)
gen.match3 <- Match(Y = outcome2, Tr = tr1, X=xcov4, Weight.matrix=gen.matched, estimand="ATT")
MatchBalance(tr1 ~ xcov4, match.out=gen.match3)

summary(gen.match3)

# outcome 1, treatment 1, and xcov3 # Model 4

set.seed(7312019)
matching <- Match(Y = outcome1, Tr = tr1, X=xcov3, ties=TRUE, estimand="ATT")
set.seed(7312019)
gen.matched <- GenMatch(Tr = tr1, X = xcov3, ties=TRUE)
gen.match4 <- Match(Y = outcome1, Tr = tr1, X=xcov3, Weight.matrix=gen.matched, estimand="ATT")
MatchBalance(tr1 ~ xcov3, match.out=gen.match4)

summary(gen.match4) 

# outcome 1, treatment 1, and xcov3b # Model 5

set.seed(7312019)
matching <- Match(Y = outcome1, Tr = tr1, X=xcov3b, ties=TRUE, estimand="ATT")
set.seed(7312019)
gen.matched <- GenMatch(Tr = tr1, X = xcov3b, ties=TRUE)
gen.match5 <- Match(Y = outcome1, Tr = tr1, X=xcov3b, Weight.matrix=gen.matched, estimand="ATT")
MatchBalance(tr1 ~ xcov3b, match.out=gen.match5)

summary(gen.match5) 

# outcome 1 treatment 1, and xcov4 # Model 6

set.seed(7312018)
matching <- Match(Y = outcome1, Tr = tr1, X=xcov4, ties=TRUE, estimand="ATT")
set.seed(7312018)
gen.matched <- GenMatch(Tr = tr1, X = xcov4, ties=TRUE)
gen.match6 <- Match(Y = outcome1, Tr = tr1, X=xcov4, Weight.matrix=gen.matched, estimand="ATT")
MatchBalance(tr1 ~ xcov4, match.out=gen.match6)

summary(gen.match6) 


## ATC RESULTS (Table 2) ##

# outcome 2, treatment 1, and xcov3 # Model 1

set.seed(7312019)
matchingC <- Match(Y = outcome2, Tr = tr1, X=xcov3, ties=TRUE, estimand="ATC")
set.seed(7312019)
gen.matched <- GenMatch(Tr = tr1, X = xcov3, ties=TRUE)
gen.match1C <- Match(Y = outcome2, Tr = tr1, X=xcov3, Weight.matrix=gen.matched, estimand="ATC")
MatchBalance(tr1 ~ xcov3, match.out=gen.match1C)

summary(gen.match1C)

# outcome 2, treatment 1, and xcov3b # Model 2

set.seed(7312019)
matchingC <- Match(Y = outcome2, Tr = tr1, X=xcov3b, ties=TRUE, estimand="ATC")
gen.matched <- GenMatch(Tr = tr1, X = xcov3b, ties=TRUE)
set.seed(7312019)
gen.match2C <- Match(Y = outcome2, Tr = tr1, X=xcov3b, Weight.matrix=gen.matched, estimand="ATC")
MatchBalance(tr1 ~ xcov3b, match.out=gen.match2C)

summary(gen.match2C) 

# outcome 2, treatment 1, and xcov4 # Model 3

set.seed(7312018)
matchingC <- Match(Y = outcome2, Tr = tr1, X=xcov4, ties=TRUE, estimand="ATC")
gen.matched <- GenMatch(Tr = tr1, X = xcov4, ties=TRUE)
set.seed(7312018)
gen.match3C <- Match(Y = outcome2, Tr = tr1, X=xcov4, Weight.matrix=gen.matched, estimand="ATC")
MatchBalance(tr1 ~ xcov4, match.out=gen.match3C)

summary(gen.match3C) 

# outcome 1, treatment 1, and xcov3 # Model 4

set.seed(7312019)
matchingC <- Match(Y = outcome1, Tr = tr1, X=xcov3, ties=TRUE, estimand="ATC")
gen.matched <- GenMatch(Tr = tr1, X = xcov3, ties=TRUE)
set.seed(7312019)
gen.match4C <- Match(Y = outcome1, Tr = tr1, X=xcov3, Weight.matrix=gen.matched, estimand="ATC")
MatchBalance(tr1 ~ xcov3, match.out=gen.match4C)

summary(gen.match4C) 


# outcome 1, treatment 1, and xcov3b # Model 5

set.seed(7312019)
matchingC <- Match(Y = outcome1, Tr = tr1, X=xcov3b, ties=TRUE, estimand="ATC")
gen.matched <- GenMatch(Tr = tr1, X = xcov3b, ties=TRUE)
set.seed(7312019)
gen.match5C <- Match(Y = outcome1, Tr = tr1, X=xcov3b, Weight.matrix=gen.matched, estimand="ATC")
MatchBalance(tr1 ~ xcov3, match.out=gen.match5C)

summary(gen.match5C) 


# outcome 1 treatment 1, and xcov4 # Model 6

set.seed(7312018)
matchingC <- Match(Y = outcome1, Tr = tr1, X=xcov4, ties=TRUE, estimand="ATC")
gen.matched <- GenMatch(Tr = tr1, X = xcov4, ties=TRUE)
set.seed(7312018)
gen.match6C <- Match(Y = outcome1, Tr = tr1, X=xcov4, Weight.matrix=gen.matched, estimand="ATC")
MatchBalance(tr1 ~ xcov4, match.out=gen.match6C)

summary(gen.match6C)



## ATE RESULTS (Table 2) ##

# outcome 2, treatment 1, and xcov3 # Model 1

set.seed(7312019)
matchingE <- Match(Y = outcome2, Tr = tr1, X=xcov3, ties=TRUE, estimand="ATE")
gen.matched <- GenMatch(Tr = tr1, X = xcov3, ties=TRUE)
set.seed(7312019)
gen.match1E <- Match(Y = outcome2, Tr = tr1, X=xcov3, Weight.matrix=gen.matched, estimand="ATE")
MatchBalance(tr1 ~ xcov3, match.out=gen.match1E)

summary(gen.match1E)

# outcome 2, treatment 1, and xcov3b # Model 2

set.seed(7312019)
matchingE <- Match(Y = outcome2, Tr = tr1, X=xcov3b, ties=TRUE, estimand="ATE")
set.seed(7312019)
gen.matched <- GenMatch(Tr = tr1, X = xcov3b, ties=TRUE)
gen.match2E <- Match(Y = outcome2, Tr = tr1, X=xcov3b, Weight.matrix=gen.matched, estimand="ATE")
MatchBalance(tr1 ~ xcov3, match.out=gen.match2E)

summary(gen.match2E) 

# outcome 2, treatment 1, and xcov4 # Model 3

set.seed(7312018)
matchingE <- Match(Y = outcome2, Tr = tr1, X=xcov4, ties=TRUE, estimand="ATE")
gen.matched <- GenMatch(Tr = tr1, X = xcov4, ties=TRUE)
set.seed(7312018)
gen.match3E <- Match(Y = outcome2, Tr = tr1, X=xcov4, Weight.matrix=gen.matched, estimand="ATE")
MatchBalance(tr1 ~ xcov4, match.out=gen.match3E)

summary(gen.match3E) 

# outcome 1, treatment 1, and xcov3 # Model 4

set.seed(7312019)
matchingE <- Match(Y = outcome1, Tr = tr1, X=xcov3, ties=TRUE, estimand="ATE")
gen.matched <- GenMatch(Tr = tr1, X = xcov3, ties=TRUE)
set.seed(7312019)
gen.match4E <- Match(Y = outcome1, Tr = tr1, X=xcov3, Weight.matrix=gen.matched, estimand="ATE")
MatchBalance(tr1 ~ xcov3, match.out=gen.match4E)

summary(gen.match4E) 


# outcome 1, treatment 1, and xcov3b # Model 5

set.seed(7312019)
matchingE <- Match(Y = outcome1, Tr = tr1, X=xcov3b, ties=TRUE, estimand="ATE")
gen.matched <- GenMatch(Tr = tr1, X = xcov3b, ties=TRUE)
set.seed(7312019)
gen.match5E <- Match(Y = outcome1, Tr = tr1, X=xcov3b, Weight.matrix=gen.matched, estimand="ATE")
MatchBalance(tr1 ~ xcov3, match.out=gen.match5E)

summary(gen.match5E)


# outcome 1 treatment 1, and xcov4 # Model 6

set.seed(7312018)
matchingE <- Match(Y = outcome1, Tr = tr1, X=xcov4, ties=TRUE, estimand="ATE")
set.seed(7312018)
gen.matched <- GenMatch(Tr = tr1, X = xcov4, ties=TRUE)
gen.match6E <- Match(Y = outcome1, Tr = tr1, X=xcov4, Weight.matrix=gen.matched, estimand="ATE")
MatchBalance(tr1 ~ xcov4, match.out=gen.match6E)

summary(gen.match6) 




##################============ TEST 2 ============##################

data2 <- read.csv("test2data.csv")

abridged <- data.frame(cbind(data2$treatment, data2$INCUMBENT, data2$OPEN, data2$IDEOLOGY, data2$moderate, data2$DIST_TO_ZERO, data2$party, 
                             data2$congress, data2$state_upper, data2$abs_closer_zero, data2$y2012, data2$y2010, data2$y2008, 
                             data2$post_reform, data2$year, data2$calif, data2$wash, data2$np_score))

colnames(abridged) <- c("treatment", "incumbent", "open", "ideology", "moderate", "candidate_ext", 
                        "party", "congress", "state_upper", "more_extreme", "y2012", "y2010", "y2008",
                        "post_reform", "year", "calif", "wash", "np_score")

abridged$dist_extreme <- abs(data2$tw_district_ideo) # how extreme the district is
abridged$dist_ideo <- data2$tw_district_ideo
abridged$np_ext <- abs(data2$np_score)


##### Regression Models #####


model4 <- glm(moderate ~ treatment + party + incumbent + open + state_upper + dist_extreme + more_extreme + factor(year), data=abridged, family='binomial')
summary(model4)

model3b <- glm(candidate_ext ~ treatment  + party +incumbent + open + state_upper + dist_extreme + more_extreme, data=abridged)
summary(model3b)

model3c <- glm(candidate_ext ~ treatment + party +incumbent + open + state_upper + dist_extreme + more_extreme + factor(year), data=abridged)
summary(model3c)

model3.1 <- glm(candidate_ext ~ treatment+  + party + incumbent + open + state_upper+ dist_extreme + more_extreme + factor(year)+ calif + wash, data=abridged)
summary(model3.1) 

model4b <- glm(moderate ~ treatment + party +incumbent + open + state_upper + dist_extreme + more_extreme,family = 'binomial', data=abridged)
summary(model4b)

model5 <- glm(moderate ~ treatment + party + incumbent + open + state_upper + dist_extreme + more_extreme + factor(year) + calif + wash, data=abridged, family='binomial')
summary(model5)

stargazer(model3b, model3c, model3.1, model4b, model4, model5, out = "table3.tex") # To Make Table 3

## Marginal Effect Plot: Figure 4 ##

p <- ggplot(subset(abridged, calif == 1), aes(candidate_ext))
pdf("figure4_calif.pdf")
p + geom_area(stat = "bin",fill = "#998EC3", alpha = .5, colour = "#998EC3", size = 1) +
  geom_vline(xintercept = .747, size = 1, linetype = "dashed", colour = "#999999") +
  geom_vline(xintercept = 0.644, size = 1, linetype = "dashed") +
  xlab("Folded CF-Score") +
  ggtitle("California") +
  theme_safeskies +
  theme(plot.title = element_text(face="bold", size = 25), axis.text=element_text(size=15), axis.title = element_text(size = 18.5),
        panel.background = element_rect(fill = "transparent", colour = NA), plot.background = element_rect(fill = "transparent", colour = NA)) 
dev.off()

p <- ggplot(subset(abridged, calif == 0), aes(candidate_ext))
pdf("figure4_wash.pdf")
p + geom_area(stat = "bin",fill = "#F1A340", alpha = .5, colour = "#F1A340", size = 1) +
  geom_vline(xintercept = 0.909, size = 1, linetype = "dashed", colour = "#999999") +
  geom_vline(xintercept = 0.806, size = 1, linetype = "dashed") +
  xlab("Folded CF-Score") +
  ggtitle("Washington") +
  theme_safeskies +
  theme(plot.title = element_text(face="bold", size = 25), axis.text=element_text(size=15), axis.title = element_text(size = 18.5),
        panel.background = element_rect(fill = "transparent", colour = NA), plot.background = element_rect(fill = "transparent", colour = NA)) 
dev.off()


##### Matching Models (Table 4) #####


match.data <- cbind(abridged$treatment, abridged$year, abridged$moderate, abridged$incumbent, abridged$open,
                    abridged$more_extreme, abridged$state_upper, abridged$dist_extreme, abridged$candidate_ext,
                    abridged$dist_ideo, abridged$party, abridged$calif, abridged$wash, abridged$y2008, 
                    abridged$y2010, abridged$y2012)
match.data <- data.frame(na.omit(match.data))
names(match.data) <- c("treatment", "year", "moderate", "incumbent", "open", "more_extreme", "state_upper",
                       "dist_extreme", "cand_extreme", "dist_ideo", "party", "calif", "wash", "y2008", 
                       "y2010", "y2012")
xcov1 <- cbind(match.data$incumbent, match.data$open, match.data$more_extreme, match.data$state_upper, match.data$dist_extreme, match.data$party)
colnames(xcov1) <- c("incumbent", "open", "more_extreme", "state_upper", "dist_extreme", "party")
xcov2 <- cbind(match.data$incumbent, match.data$open, match.data$more_extreme, match.data$state_upper, match.data$dist_extreme, match.data$party, match.data$year)
colnames(xcov2) <- c("incumbent", "open", "more_extreme", "state_upper", "dist_extreme", "party", "year")
xcov3 <- cbind(match.data$incumbent, match.data$open, match.data$more_extreme, match.data$state_upper, match.data$dist_extreme, match.data$party, match.data$calif, match.data$wash)
colnames(xcov3) <- c("incumbent", "open", "more_extreme", "state_upper", "dist_extreme", "party", "calif", "wash")
xcov4 <- cbind(match.data$incumbent, match.data$open, match.data$more_extreme, match.data$state_upper, match.data$dist_extreme, match.data$party, match.data$calif, match.data$wash, match.data$year)
colnames(xcov4) <- c("incumbent", "open", "more_extreme", "state_upper", "dist_extreme", "party", "calif", "wash", "year")

outcome1 <- match.data$moderate
outcome2 <- match.data$cand_extreme
tr1 <- match.data$treatment


## ATT RESULTS (Table 4) ##

# outcome 2, treatment 1, and xcov1; Model 1

set.seed(07312019)
matching <- Match(Y = outcome2, Tr = tr1, X=xcov1, ties=TRUE, estimand="ATT")
gen.matched <- GenMatch(Tr = tr1, X = xcov1, ties=TRUE)
set.seed(07312019)
gen.match1 <- Match(Y = outcome2, Tr = tr1, X=xcov1, Weight.matrix=gen.matched, estimand="ATT")
MatchBalance(tr1 ~ xcov1, match.out=gen.match1)

summary(gen.match1) 


# outcome 2, treatment 1, and xcov2; Model 2 

set.seed(07312018)
matching <- Match(Y = outcome2, Tr = tr1, X=xcov2, ties=TRUE, estimand="ATT")
gen.matched <- GenMatch(Tr = tr1, X = xcov2, ties=TRUE)
set.seed(07312018)
gen.match2 <- Match(Y = outcome2, Tr = tr1, X=xcov2, Weight.matrix=gen.matched, estimand="ATT")
MatchBalance(tr1 ~ xcov2, match.out=gen.match2)

summary(gen.match2) 

# outcome 2, treatment 1, and xcov4; Model 3 

set.seed(07312018)
matching <- Match(Y = outcome2, Tr = tr1, X=xcov4, ties=TRUE, estimand="ATT")
gen.matched <- GenMatch(Tr = tr1, X = xcov4, ties=TRUE)
set.seed(07312018)
gen.match3 <- Match(Y = outcome2, Tr = tr1, X=xcov4, Weight.matrix=gen.matched, estimand="ATT")
MatchBalance(tr1 ~ xcov4, match.out=gen.match3)

summary(gen.match3)

# outcome 1, treatment 1, and xcov1; Model 4 

set.seed(07312018)
matching <- Match(Y = outcome1, Tr = tr1, X=xcov1, ties=TRUE, estimand="ATT")
gen.matched <- GenMatch(Tr = tr1, X = xcov1, ties=TRUE)
set.seed(07312018)
gen.match4 <- Match(Y = outcome1, Tr = tr1, X=xcov1, Weight.matrix=gen.matched, estimand="ATT")
MatchBalance(tr1 ~ xcov1, match.out=gen.match4)

summary(gen.match4) 

# outcome 1, treatment 1, and xcov2; Model 5 

set.seed(07312018)
matching <- Match(Y = outcome1, Tr = tr1, X=xcov2, ties=TRUE, estimand="ATT")
gen.matched <- GenMatch(Tr = tr1, X = xcov2, ties=TRUE)
set.seed(07312018)
gen.match5 <- Match(Y = outcome1, Tr = tr1, X=xcov2, Weight.matrix=gen.matched, estimand="ATT")
MatchBalance(tr1 ~ xcov2, match.out=gen.match5)

summary(gen.match5)

# outcome 1, treatment 1, and xcov4; Model 6 

set.seed(07312018)
matching <- Match(Y = outcome1, Tr = tr1, X=xcov4, ties=TRUE, estimand="ATT")
gen.matched <- GenMatch(Tr = tr1, X = xcov4, ties=TRUE)
set.seed(07312018)
gen.match6 <- Match(Y = outcome1, Tr = tr1, X=xcov4, Weight.matrix=gen.matched, estimand="ATT")
MatchBalance(tr1 ~ xcov4, match.out=gen.match6)

summary(gen.match6)


## ATC RESULTS (Table 4) ##

# outcome 2, treatment 1, and xcov1; Model 1 

set.seed(07312018)
matchingC <- Match(Y = outcome2, Tr = tr1, X=xcov1, ties=TRUE, estimand="ATC")
gen.matched <- GenMatch(Tr = tr1, X = xcov1, ties=TRUE)
set.seed(07312018)
gen.match1C <- Match(Y = outcome2, Tr = tr1, X=xcov1, Weight.matrix=gen.matched, estimand="ATC")
MatchBalance(tr1 ~ xcov1, match.out=gen.match1C)

summary(gen.match1C)


# outcome 2, treatment 1, and xcov2; Model 2 

set.seed(08012018)
matchingC <- Match(Y = outcome2, Tr = tr1, X=xcov2, ties=TRUE, estimand="ATC")
gen.matched <- GenMatch(Tr = tr1, X = xcov2, ties=TRUE)
set.seed(08012018)
gen.match2C <- Match(Y = outcome2, Tr = tr1, X=xcov2, Weight.matrix=gen.matched, estimand="ATC")
MatchBalance(tr1 ~ xcov2, match.out=gen.match2C)

summary(gen.match2C)

# outcome 2, treatment 1, and xcov4; Model 3

set.seed(08012018)
matchingC <- Match(Y = outcome2, Tr = tr1, X=xcov4, ties=TRUE, estimand="ATC")
gen.matched <- GenMatch(Tr = tr1, X = xcov4, ties=TRUE)
set.seed(08012018)
gen.match3C <- Match(Y = outcome2, Tr = tr1, X=xcov4, Weight.matrix=gen.matched, estimand="ATC")
MatchBalance(tr1 ~ xcov4, match.out=gen.match3C)

summary(gen.match3C) 

# outcome 1, treatment 1, and xcov1; Model 4 

set.seed(08012018)
matchingC <- Match(Y = outcome1, Tr = tr1, X=xcov1, ties=TRUE, estimand="ATC")
gen.matched <- GenMatch(Tr = tr1, X = xcov1, ties=TRUE)
set.seed(08012018)
gen.match4C <- Match(Y = outcome1, Tr = tr1, X=xcov1, Weight.matrix=gen.matched, estimand="ATC")
MatchBalance(tr1 ~ xcov1, match.out=gen.match4C)

summary(gen.match4C)

# outcome 1, treatment 1, and xcov2; Model 5 

set.seed(08012018)
matchingC <- Match(Y = outcome1, Tr = tr1, X=xcov2, ties=TRUE, estimand="ATC")
gen.matched <- GenMatch(Tr = tr1, X = xcov2, ties=TRUE)
set.seed(08012018)
gen.match5C <- Match(Y = outcome1, Tr = tr1, X=xcov2, Weight.matrix=gen.matched, estimand="ATC")
MatchBalance(tr1 ~ xcov2, match.out=gen.match5C)

summary(gen.match5C) 

# outcome 1, treatment 1, and xcov4; Model 6

set.seed(08012018)
matchingC <- Match(Y = outcome1, Tr = tr1, X=xcov4, ties=TRUE, estimand="ATC")
gen.matched <- GenMatch(Tr = tr1, X = xcov4, ties=TRUE)
set.seed(08012018)
gen.match6C <- Match(Y = outcome1, Tr = tr1, X=xcov4, Weight.matrix=gen.matched, estimand="ATC")
MatchBalance(tr1 ~ xcov4, match.out=gen.match6C)

summary(gen.match6C) 


## ATE RESULTS (Table 4) ##

# outcome 2, treatment 1, and xcov1; Model 1

set.seed(08012018)
matchingE <- Match(Y = outcome2, Tr = tr1, X=xcov1, ties=TRUE, estimand="ATE")
gen.matched <- GenMatch(Tr = tr1, X = xcov1, ties=TRUE)
set.seed(08012018)
gen.match1E <- Match(Y = outcome2, Tr = tr1, X=xcov1, Weight.matrix=gen.matched, estimand="ATE")
MatchBalance(tr1 ~ xcov1, match.out=gen.match1E)

summary(gen.match1E) 

# outcome 2, treatment 1, and xcov2; Model 2 

set.seed(08012018)
matchingE <- Match(Y = outcome2, Tr = tr1, X=xcov2, ties=TRUE, estimand="ATE")
gen.matched <- GenMatch(Tr = tr1, X = xcov2, ties=TRUE)
set.seed(08012018)
gen.match2E <- Match(Y = outcome2, Tr = tr1, X=xcov2, Weight.matrix=gen.matched, estimand="ATE")
MatchBalance(tr1 ~ xcov2, match.out=gen.match2E)

summary(gen.match2E) 

# outcome 2, treatment 1, and xcov4; Model 3

set.seed(08012018)
matchingE <- Match(Y = outcome2, Tr = tr1, X=xcov4, ties=TRUE, estimand="ATE")
gen.matched <- GenMatch(Tr = tr1, X = xcov4, ties=TRUE)
set.seed(08012018)
gen.match3E <- Match(Y = outcome2, Tr = tr1, X=xcov4, Weight.matrix=gen.matched, estimand="ATE")
MatchBalance(tr1 ~ xcov4, match.out=gen.match3E)

summary(gen.match3E) 

# outcome 1, treatment 1, and xcov1; Model 4

set.seed(08012018)
matchingE <- Match(Y = outcome1, Tr = tr1, X=xcov1, ties=TRUE, estimand="ATE")
gen.matched <- GenMatch(Tr = tr1, X = xcov1, ties=TRUE)
set.seed(08012018)
gen.match4E <- Match(Y = outcome1, Tr = tr1, X=xcov1, Weight.matrix=gen.matched, estimand="ATE")
MatchBalance(tr1 ~ xcov1, match.out=gen.match4E)

summary(gen.match4E) 

# outcome 1, treatment 1, and xcov2; Model 5

set.seed(08012018)
matchingE <- Match(Y = outcome1, Tr = tr1, X=xcov2, ties=TRUE, estimand="ATE")
gen.matched <- GenMatch(Tr = tr1, X = xcov2, ties=TRUE)
set.seed(08012018)
gen.match5E <- Match(Y = outcome1, Tr = tr1, X=xcov2, Weight.matrix=gen.matched, estimand="ATE")
MatchBalance(tr1 ~ xcov2, match.out=gen.match5E)

summary(gen.match5E) 

# outcome 1, treatment 1, and xcov4; Model 6 

set.seed(08012018)
matchingE <- Match(Y = outcome1, Tr = tr1, X=xcov4, ties=TRUE, estimand="ATE")
gen.matched <- GenMatch(Tr = tr1, X = xcov4, ties=TRUE)
set.seed(08012018)
gen.match6E <- Match(Y = outcome1, Tr = tr1, X=xcov4, Weight.matrix=gen.matched, estimand="ATE")
MatchBalance(tr1 ~ xcov4, match.out=gen.match6E)

summary(gen.match6E) 





##############=========== TEST 3 ==========###############

data3.1 <- read.csv("final_merged_data2shor.csv")

abridged1 <- data.frame(cbind(data3.1$treatment, data3.1$in_analysis, data3.1$INCUMBENT, data3.1$OPEN, data3.1$IDEOLOGY, data3.1$moderate, data3.1$DIST_TO_ZERO, data3.1$Party,
                              data3.1$congress, data3.1$state_upper, data3.1$abs_closer_zero, data3.1$year2012, data3.1$year2010, data3.1$year2008, data3.1$toptwo, data3.1$calif, data3.1$wash, data3.1$YEAR))

colnames(abridged1) <- c("treatment", "in_analysis", "incumbent", "open", "ideology", "moderate", "candidate_ext", "party", "congress", "state_upper", "more_extreme", "y2012", "y2010", "y2008", "toptwo", "calif", "wash", "year")

abridged1$dist_extreme <- abs(data3.1$tw_district_ideo) # how extreme the district is
abridged1$dist_ideo <- data3.1$tw_district_ideo


##### Regression Models #####

model3 <- glm(moderate ~ treatment + party + incumbent + open + state_upper + dist_extreme + more_extreme, data=abridged1, family='binomial')
summary(model3)

model3.FE <- glm(moderate ~ treatment + party + incumbent + open + state_upper + dist_extreme + more_extreme+ calif + wash, data=abridged1, family='binomial')
summary(model3.FE) 

model3.FEb <- glm(moderate ~ treatment + party + incumbent + open + state_upper + dist_extreme + more_extreme + calif + wash + factor(year), data=abridged1, family='binomial')
summary(model3.FEb) 

model4 <- glm(moderate ~ treatment + toptwo + party + incumbent + open + state_upper + dist_extreme + more_extreme + factor(year), data=abridged1, family='binomial')
summary(model4)

model3.1 <- glm(candidate_ext ~ treatment + party + incumbent + open + state_upper + dist_extreme + more_extreme, data=abridged1)
summary(model3.1)

model3.1FE <- glm(candidate_ext ~ treatment + party + incumbent + open + state_upper + dist_extreme + more_extreme+ calif + wash, data=abridged1)
summary(model3.1FE)

model3.1FEb <- glm(candidate_ext ~ treatment + party + incumbent + open + state_upper + dist_extreme + more_extreme + calif + wash + factor(year), data=abridged1)
summary(model3.1FEb) 

model4.1 <- glm(candidate_ext ~ treatment + toptwo + party + incumbent + open + state_upper + dist_extreme + more_extreme + factor(year), data=abridged1)
summary(model4.1)

# Code to make Table 5
stargazer(model3.1, model3.1FE, model3.1FEb, model4.1, model3, model3.FE, model3.FEb, model4, out = "table5.tex") 

## Marginal Effects Plots: Figure 5 ##

p <- ggplot(subset(abridged1, calif == 1), aes(candidate_ext))
pdf("figure5_calif.pdf")
p + geom_area(stat = "bin",fill = "#998EC3", alpha = .5, colour = "#998EC3", size = 1) +
  geom_vline(xintercept = 0.765 , size = 1, linetype = "dashed", colour = "#999999") +
  geom_vline(xintercept = 0.642, size = 1, linetype = "dashed") +
  xlab("Folded CF-Score") +
  ggtitle("California") +
  theme_safeskies +
  theme(plot.title = element_text(face="bold", size = 25), axis.text=element_text(size=15), axis.title = element_text(size = 18.5),
        panel.background = element_rect(fill = "transparent", colour = NA), plot.background = element_rect(fill = "transparent", colour = NA)) 
ggsave("california_test3.png", p, bg = "transparent")
dev.off()

p <- ggplot(subset(abridged1, calif == 0), aes(candidate_ext))
pdf("figure5_wash.pdf")
p + geom_area(stat = "bin",fill = "#F1A340", alpha = .5, colour = "#F1A340", size = 1) +
  geom_vline(xintercept = 0.897, size = 1, linetype = "dashed", colour = "#999999") +
  geom_vline(xintercept = 0.773, size = 1, linetype = "dashed") +
  xlab("Folded CF-Score") +
  ggtitle("Washington") +
  theme_safeskies +
  theme(plot.title = element_text(face="bold", size = 25), axis.text=element_text(size=15), axis.title = element_text(size = 18.5),
        panel.background = element_rect(fill = "transparent", colour = NA), plot.background = element_rect(fill = "transparent", colour = NA)) 
dev.off()

###### Matching Models (Table 6) #####

# The Data

match.data <- cbind(as.numeric(as.character(abridged1$treatment)), as.numeric(as.character(abridged1$moderate)), as.numeric(as.character(abridged1$incumbent)), as.numeric(as.character(abridged1$open)), abridged1$more_extreme, 
                    abridged1$state_upper, abridged1$dist_extreme, abridged1$candidate_ext, abridged1$dist_ideo, abridged1$party,
                    abridged1$calif, abridged1$wash, abridged1$y2008, abridged1$y2010, abridged1$y2012, abridged1$toptwo, abridged1$year)
match.data <- data.frame(na.omit(match.data))
names(match.data) <- c("treatment", "moderate", "incumbent", "open", "more_extreme", "state_upper", "dist_extreme",
                       "cand_extreme", "dist_ideo", "party", "calif", "wash", "y2008", "y2010", "y2012", "toptwo", "year")

xcov1 <- cbind(match.data$incumbent, match.data$open, match.data$more_extreme, match.data$state_upper, match.data$dist_extreme, match.data$party)
colnames(xcov1) <- c("incumbent", "open", "more_extreme", "state_upper", "dist_extreme", "party")
xcov2 <- cbind(match.data$incumbent, match.data$open, match.data$more_extreme, match.data$state_upper, match.data$dist_extreme, match.data$party, match.data$year)
colnames(xcov2) <- c("incumbent", "open", "more_extreme", "state_upper", "dist_extreme", "party", "year")
xcov3 <- cbind(match.data$incumbent, match.data$open, match.data$more_extreme, match.data$state_upper, match.data$dist_extreme, match.data$party, match.data$calif, match.data$wash)
colnames(xcov3) <- c("incumbent", "open", "more_extreme", "state_upper", "dist_extreme", "party", "calif", "wash")
xcov4 <- cbind(match.data$incumbent, match.data$open, match.data$more_extreme, match.data$state_upper, match.data$dist_extreme, match.data$party, match.data$calif, match.data$wash, match.data$year)
colnames(xcov4) <- c("incumbent", "open", "more_extreme", "state_upper", "dist_extreme", "party", "calif", "wash", "year")
xcov5 <- cbind(match.data$incumbent, match.data$open, match.data$more_extreme, match.data$state_upper, match.data$dist_extreme, match.data$party, match.data$year, match.data$toptwo)
colnames(xcov5) <- c("incumbent", "open", "more_extreme", "state_upper", "dist_extreme", "party", "year", "toptwo")

outcome1 <- match.data$moderate
outcome2 <- match.data$cand_extreme
tr1 <- match.data$treatment


## ATT RESULTS (Table 6) ##

# outcome 2, treatment 1, and xcov1; Model 1 

require(Matching)
set.seed(08012018)
matching <- Match(Y = outcome2, Tr = tr1, X=xcov1, ties=TRUE, estimand="ATT")
gen.matched <- GenMatch(Tr = tr1, X = xcov1, ties=TRUE)
set.seed(08012018)
gen.match1 <- Match(Y = outcome2, Tr = tr1, X=xcov1, Weight.matrix=gen.matched, estimand="ATT")
MatchBalance(tr1 ~ xcov1, match.out=gen.match1)

summary(gen.match1) 

# outcome 2, treatment 1, and xcov3; Model 2 

set.seed(08012018)
matching <- Match(Y = outcome2, Tr = tr1, X=xcov3, ties=TRUE, estimand="ATT")
gen.matched <- GenMatch(Tr = tr1, X = xcov3, ties=TRUE)
set.seed(08012018)
gen.match2 <- Match(Y = outcome2, Tr = tr1, X=xcov3, Weight.matrix=gen.matched, estimand="ATT")
MatchBalance(tr1 ~ xcov3, match.out=gen.match2)

summary(gen.match2) 

# outcome 2, treatment 1, and xcov4; Model 3 

set.seed(080220181)
matching <- Match(Y = outcome2, Tr = tr1, X=xcov4, ties=TRUE, estimand="ATT")
gen.matched <- GenMatch(Tr = tr1, X = xcov4, ties=TRUE)
set.seed(080220181)
gen.match3 <- Match(Y = outcome2, Tr = tr1, X=xcov4, Weight.matrix=gen.matched, estimand="ATT")
MatchBalance(tr1 ~ xcov4, match.out=gen.match3)

summary(gen.match3) 

# outcome 2, treatment 1, and xcov5, as well as toptwo; Model 4 

set.seed(08022018)
matching <- Match(Y = outcome2, Tr = tr1, X=xcov5, ties=TRUE, estimand="ATT")
gen.matched <- GenMatch(Tr = tr1, X = xcov5, ties=TRUE)
set.seed(08022018)
gen.match4 <- Match(Y = outcome2, Tr = tr1, X=xcov5, Weight.matrix=gen.matched, estimand="ATT")
MatchBalance(tr1 ~ xcov5, match.out=gen.match4)

summary(gen.match4)

# outcome 1, treatment 1, and xcov1; Model 5 

set.seed(08022018)
matching <- Match(Y = outcome1, Tr = tr1, X=xcov1, ties=TRUE, estimand="ATT")
gen.matched <- GenMatch(Tr = tr1, X = xcov1, ties=TRUE)
set.seed(08022018)
gen.match5 <- Match(Y = outcome1, Tr = tr1, X=xcov1, Weight.matrix=gen.matched, estimand="ATT")
MatchBalance(tr1 ~ xcov1, match.out=gen.match5)

summary(gen.match5) 

# outcome 1, treatment 1, and xcov3; Model 6 

set.seed(08022018)
matching <- Match(Y = outcome1, Tr = tr1, X=xcov3, ties=TRUE, estimand="ATT")
gen.matched <- GenMatch(Tr = tr1, X = xcov3, ties=TRUE)
set.seed(08022018)
gen.match6 <- Match(Y = outcome1, Tr = tr1, X=xcov3, Weight.matrix=gen.matched, estimand="ATT")
MatchBalance(tr1 ~ xcov3, match.out=gen.match6)

summary(gen.match6) 

# outcome 1, treatment 1, and xcov4; Model 7 

set.seed(08022018)
matching <- Match(Y = outcome1, Tr = tr1, X=xcov4, ties=TRUE, estimand="ATT")
gen.matched <- GenMatch(Tr = tr1, X = xcov4, ties=TRUE)
set.seed(08022018)
gen.match7 <- Match(Y = outcome1, Tr = tr1, X=xcov4, Weight.matrix=gen.matched, estimand="ATT")
MatchBalance(tr1 ~ xcov4, match.out=gen.match7)

summary(gen.match7) 

# outcome 1, treatment 1, and xcov5; Model 8 

set.seed(08022018)
matching <- Match(Y = outcome1, Tr = tr1, X=xcov5, ties=TRUE, estimand="ATT")
gen.matched <- GenMatch(Tr = tr1, X = xcov5, ties=TRUE)
set.seed(08022018)
gen.match8 <- Match(Y = outcome1, Tr = tr1, X=xcov5, Weight.matrix=gen.matched, estimand="ATT")
MatchBalance(tr1 ~ xcov5, match.out=gen.match8)

summary(gen.match8) 


## ATC RESULTS (Table 6) ##

# outcome 2, treatment 1, and xcov1; Model 1 

set.seed(08022018)
matchingC <- Match(Y = outcome2, Tr = tr1, X=xcov1, ties=TRUE, estimand="ATC")
gen.matched <- GenMatch(Tr = tr1, X = xcov1, ties=TRUE)
set.seed(08022018)
gen.match1C <- Match(Y = outcome2, Tr = tr1, X=xcov1, Weight.matrix=gen.matched, estimand="ATC")
MatchBalance(tr1 ~ xcov1, match.out=gen.match1C)

summary(gen.match1C) 


# outcome 2, treatment 1, and xcov3; Model 2 

set.seed(08012018)
matchingC <- Match(Y = outcome2, Tr = tr1, X=xcov3, ties=TRUE, estimand="ATC")
gen.matched <- GenMatch(Tr = tr1, X = xcov3, ties=TRUE)
set.seed(08012018)
gen.match2C <- Match(Y = outcome2, Tr = tr1, X=xcov3, Weight.matrix=gen.matched, estimand="ATC")
MatchBalance(tr1 ~ xcov3, match.out=gen.match2C)

summary(gen.match2C) 

# outcome 2, treatment 1, and xcov4; Model 3 

set.seed(08012018)
matchingC <- Match(Y = outcome2, Tr = tr1, X=xcov4, ties=TRUE, estimand="ATC")
gen.matched <- GenMatch(Tr = tr1, X = xcov4, ties=TRUE)
set.seed(08012018)
gen.match3C <- Match(Y = outcome2, Tr = tr1, X=xcov4, Weight.matrix=gen.matched, estimand="ATC")
MatchBalance(tr1 ~ xcov4, match.out=gen.match3C)

summary(gen.match3C) 

# outcome 2, treatment 1, and xcov5, as well as toptwo; Model 4 

set.seed(08012018)
matchingC <- Match(Y = outcome2, Tr = tr1, X=xcov5, ties=TRUE, estimand="ATC")
gen.matched <- GenMatch(Tr = tr1, X = xcov5, ties=TRUE)
set.seed(08012018)
gen.match4C <- Match(Y = outcome2, Tr = tr1, X=xcov5, Weight.matrix=gen.matched, estimand="ATC")
MatchBalance(tr1 ~ xcov5, match.out=gen.match4C)

summary(gen.match4C) 

# outcome 1, treatment 1, and xcov1; Model 5 

set.seed(08012018)
matchingC <- Match(Y = outcome1, Tr = tr1, X=xcov1, ties=TRUE, estimand="ATC")
gen.matched <- GenMatch(Tr = tr1, X = xcov1, ties=TRUE)
set.seed(08012018)
gen.match5C <- Match(Y = outcome1, Tr = tr1, X=xcov1, Weight.matrix=gen.matched, estimand="ATC")
MatchBalance(tr1 ~ xcov1, match.out=gen.match5C)

summary(gen.match5C) 

# outcome 1, treatment 1, and xcov3; Model 6 

set.seed(08012018)
matchingC <- Match(Y = outcome1, Tr = tr1, X=xcov3, ties=TRUE, estimand="ATC")
gen.matched <- GenMatch(Tr = tr1, X = xcov3, ties=TRUE)
set.seed(08012018)
gen.match6C <- Match(Y = outcome1, Tr = tr1, X=xcov3, Weight.matrix=gen.matched, estimand="ATC")
MatchBalance(tr1 ~ xcov3, match.out=gen.match6C)

summary(gen.match6C) 

# outcome 1, treatment 1, and xcov4; Model 7 

set.seed(08012018)
matchingC <- Match(Y = outcome1, Tr = tr1, X=xcov4, ties=TRUE, estimand="ATC")
gen.matched <- GenMatch(Tr = tr1, X = xcov4, ties=TRUE)
set.seed(08012018)
gen.match7C <- Match(Y = outcome1, Tr = tr1, X=xcov4, Weight.matrix=gen.matched, estimand="ATC")
MatchBalance(tr1 ~ xcov4, match.out=gen.match7C)

summary(gen.match7C) 

# outcome 1, treatment 1, and xcov5; Model 8 

set.seed(08012018)
matchingC <- Match(Y = outcome1, Tr = tr1, X=xcov5, ties=TRUE, estimand="ATC")
gen.matched <- GenMatch(Tr = tr1, X = xcov5, ties=TRUE)
set.seed(08012018)
gen.match8C <- Match(Y = outcome1, Tr = tr1, X=xcov5, Weight.matrix=gen.matched, estimand="ATC")
MatchBalance(tr1 ~ xcov5, match.out=gen.match8C)

summary(gen.match8C) 


## ATE RESULTS (Table 6) ##

# outcome 2, treatment 1, and xcov1; Model 1 

set.seed(08012018)
matchingE <- Match(Y = outcome2, Tr = tr1, X=xcov1, ties=TRUE, estimand="ATE")
gen.matched <- GenMatch(Tr = tr1, X = xcov1, ties=TRUE)
set.seed(08012018)
gen.match1E <- Match(Y = outcome2, Tr = tr1, X=xcov1, Weight.matrix=gen.matched, estimand="ATE")
MatchBalance(tr1 ~ xcov1, match.out=gen.match1E)

summary(gen.match1E) 


# outcome 2, treatment 1, and xcov3; Model 2 

set.seed(08012018)
matchingE <- Match(Y = outcome2, Tr = tr1, X=xcov3, ties=TRUE, estimand="ATE")
gen.matched <- GenMatch(Tr = tr1, X = xcov3, ties=TRUE)
set.seed(08012018)
gen.match2E <- Match(Y = outcome2, Tr = tr1, X=xcov3, Weight.matrix=gen.matched, estimand="ATE")
MatchBalance(tr1 ~ xcov3, match.out=gen.match2E)

summary(gen.match2E) 

# outcome 2, treatment 1, and xcov4; Model 3 

set.seed(08012018)
matchingE <- Match(Y = outcome2, Tr = tr1, X=xcov4, ties=TRUE, estimand="ATE")
gen.matched <- GenMatch(Tr = tr1, X = xcov4, ties=TRUE)
set.seed(08012018)
gen.match3E <- Match(Y = outcome2, Tr = tr1, X=xcov4, Weight.matrix=gen.matched, estimand="ATE")
MatchBalance(tr1 ~ xcov4, match.out=gen.match3E )

summary(gen.match3E) 

# outcome 2, treatment 1, and xcov5, as well as toptwo; Model 4 

# outcome 2, treatment 1, and xcov5, as well as toptwo; Model 4 

set.seed(08012018)
matchingC <- Match(Y = outcome2, Tr = tr1, X=xcov5, ties=TRUE, estimand="ATE")
gen.matched <- GenMatch(Tr = tr1, X = xcov5, ties=TRUE)
set.seed(08012018)
gen.match4E <- Match(Y = outcome2, Tr = tr1, X=xcov5, Weight.matrix=gen.matched, estimand="ATE")
MatchBalance(tr1 ~ xcov5, match.out=gen.match4E)

summary(gen.match4E) 

# outcome 1, treatment 1, and xcov1; Model 5 

set.seed(08012018)
matchingE <- Match(Y = outcome1, Tr = tr1, X=xcov1, ties=TRUE, estimand="ATE")
gen.matched <- GenMatch(Tr = tr1, X = xcov1, ties=TRUE)
set.seed(08012018)
gen.match5E <- Match(Y = outcome1, Tr = tr1, X=xcov1, Weight.matrix=gen.matched, estimand="ATE")
MatchBalance(tr1 ~ xcov1, match.out=gen.match5E)

summary(gen.match5E) 

# outcome 1, treatment 1, and xcov3; Model 6 

set.seed(08012018)
matchingE <- Match(Y = outcome1, Tr = tr1, X=xcov3, ties=TRUE, estimand="ATE")
gen.matched <- GenMatch(Tr = tr1, X = xcov3, ties=TRUE)
set.seed(08012018)
gen.match6E <- Match(Y = outcome1, Tr = tr1, X=xcov3, Weight.matrix=gen.matched, estimand="ATE")
MatchBalance(tr1 ~ xcov3, match.out=gen.match6E)

summary(gen.match6E) 

# outcome 1, treatment 1, and xcov4; Model 7 

set.seed(08012018)
matchingE <- Match(Y = outcome1, Tr = tr1, X=xcov4, ties=TRUE, estimand="ATE")
gen.matched <- GenMatch(Tr = tr1, X = xcov4, ties=TRUE)
set.seed(08012018)
gen.match7E <- Match(Y = outcome1, Tr = tr1, X=xcov4, Weight.matrix=gen.matched, estimand="ATE")
MatchBalance(tr1 ~ xcov4, match.out=gen.match7E)

summary(gen.match7E) 

# outcome 1, treatment 1, and xcov5; Model 8 

set.seed(08012018)
matchingE <- Match(Y = outcome1, Tr = tr1, X=xcov5, ties=TRUE, estimand="ATE")
gen.matched <- GenMatch(Tr = tr1, X = xcov5, ties=TRUE)
set.seed(08012018)
gen.match8E <- Match(Y = outcome1, Tr = tr1, X=xcov5, Weight.matrix=gen.matched, estimand="ATE")
MatchBalance(tr1 ~ xcov5, match.out=gen.match8E)

summary(gen.match8E) 



################# Sorting Analysis ######################

## Figure 6 (McCrary Test)

rd_data <- read.csv("rd_data.csv")

new.data <- NA
for(i in unique(as.character(rd_data$seat_identifier))){
  results <- subset(rd_data, seat_identifier==i)
  running_var <- NA
  vote_share <- results$general.election.vote.share
  prim_vote_share <- results$primary.vote.share
  nprim <- length(prim_vote_share)
  winner_row <- which(prim_vote_share == sort(prim_vote_share,partial=nprim)[nprim])
  gen_winner_row <- which(results$WINNER == 1)
  first_loser <- ifelse(nrow(results)>1, which(prim_vote_share == sort(prim_vote_share,partial=nprim-1)[nprim-1]),NA) 
  second_loser <- ifelse(nrow(results)>2, which(prim_vote_share == sort(prim_vote_share,partial=nprim-2)[nprim-2]),NA)
  ifelse(is.na(second_loser)==T, running_var <- NA,
         ifelse(results[winner_row,16]==results[first_loser, 16],running_var <- results[first_loser,15] - results[second_loser,15], 
                ifelse(is.na(second_loser)==T,running_var <- results[first_loser,5], running_var <- -1*(results[first_loser,15] - results[second_loser,15]))))                                                               
  results[gen_winner_row, "running_var"] <- running_var
  new.data <- rbind(new.data, results[gen_winner_row,])
}

running_var <- na.omit(new.data$running_var)

require("rdd")

pdf("figure6.pdf")
DCdensity(runvar = as.numeric(as.character(running_var)), cutpoint=0)
dev.off()

## Figure 7 (Predictors of Same-Party Competition)

data2 <- read.csv("test2data.csv")

abridged <- data.frame(cbind(data2$treatment, data2$INCUMBENT, data2$OPEN, data2$IDEOLOGY, data2$moderate, data2$DIST_TO_ZERO, data2$party, 
                             data2$congress, data2$state_upper, data2$abs_closer_zero, data2$y2012, data2$y2010, data2$y2008, 
                             data2$post_reform, data2$year, data2$calif, data2$wash, data2$np_score))
colnames(abridged) <- c("treatment", "incumbent", "open", "ideology", "moderate", "candidate_ext", 
                        "party", "congress", "state_upper", "more_extreme", "y2012", "y2010", "y2008",
                        "post_reform", "year", "calif", "wash", "np_score")
abridged$dist_extreme <- abs(data2$tw_district_ideo) # how extreme the district is
abridged$dist_ideo <- data2$tw_district_ideo
abridged$np_ext <- abs(data2$np_score)

modela <- glm(treatment ~ scale(open) + scale(dist_extreme) + party + year, family = binomial, data=abridged)
summary(modela)

require("coefplot")

p <- coefplot(modela, title = NULL,intercept = F, 
              newNames = c("year" = "Year", "party" = "Party", "scale(dist_extreme)" = "District Extremity", "scale(open)" = "Open Seat"),
              pointSize = 5,
              lwdInner = 0,
              lwdOuter = 2,
              color = "#998EC3")

pdf("figure7.pdf")
p + xlab("Coefficient") +
  theme(plot.title = element_text(face="bold", size = 25), axis.text=element_text(size=20), axis.title = element_text(size = 20.5),
        panel.background = element_rect(fill = "transparent", colour = NA), plot.background = element_rect(fill = "transparent", colour = NA)) 
dev.off()




################### APPENDIX MATERIALS #################################

## Appendix A

# Total Receipts

data2 <- read.csv("test2data.csv")

abridged <- data.frame(cbind(data2$general.election.vote.share, data2$total.receipts, data2$treatment, data2$INCUMBENT, data2$OPEN, data2$IDEOLOGY, data2$moderate, data2$DIST_TO_ZERO, data2$party, 
                             data2$congress, data2$state_upper, data2$abs_closer_zero, data2$y2012, data2$y2010, data2$y2008, 
                             data2$post_reform, data2$year, data2$calif, data2$wash))

colnames(abridged) <- c("general.election.vote.share", "total.receipts", "treatment", "incumbent", "open", "ideology", "moderate", "candidate_ext", "party", "congress", "state_upper", "more_extreme", "y2012", "y2010", "y2008", "post_reform", "year", "calif", "wash")

abridged$dist_extreme <- abs(data2$tw_district_ideo) # how extreme the district is
abridged$dist_ideo <- data2$tw_district_ideo

model3.1 <- glm(candidate_ext ~ treatment + total.receipts + congress +  party + incumbent + open + state_upper + dist_extreme + more_extreme, data=abridged)
summary(model3.1)

model4.1 <- glm(candidate_ext ~ treatment+ total.receipts + party + congress +incumbent + open + state_upper + dist_extreme + more_extreme + factor(year), data=abridged)
summary(model4.1) 

model3.1FE <- glm(candidate_ext ~ treatment+ total.receipts + party +congress + incumbent + open + state_upper + dist_extreme + more_extreme+ calif + wash, data=abridged)
summary(model3.1FE) 

model3.1FEb <- glm(candidate_ext ~ treatment+ total.receipts + party + congress +incumbent + open + state_upper + dist_extreme + more_extreme + calif + wash + factor(year), data=abridged)
summary(model3.1FEb) 

stargazer(model3.1, model4.1, model3.1FE, model3.1FEb, out = "tableA1.tex")  # Table A1

# DW-DIME

data1 <- read.csv("test1_dw.csv")
abridged <- data.frame(cbind(data1$year, data1$treatment, data1$INCUMBENT, data1$OPEN, data1$IDEOLOGY, data1$moderate, data1$DIST_TO_ZERO, data1$party, 
                             data1$congress, data1$state_upper, data1$abs_closer_zero, data1$y2012, data1$y2010, data1$y2008, data1$post_reform, 
                             data1$calif, data1$wash, data1$np_score, data1$dw_dime))

colnames(abridged) <- c("year", "treatment", "incumbent", "open", "ideology", "moderate", "candidate_ext", "party", "congress", 
                        "state_upper", "more_extreme", "y2012", "y2010", "y2008", "post_reform", "calif", "wash",
                        "np_score")

abridged$dist_extreme <- abs(data1$tw_district_ideo) # how extreme the district is
abridged$dist_ideo <- data1$tw_district_ideo
abridged$abs_np_score <- abs(data1$np_score)
abridged$abs_dwd_score <- abs(data1$dw_dime)

model3b.FE_dw1 <- glm(abs_dwd_score ~ treatment + party +incumbent + open + state_upper + dist_extreme + more_extreme + calif, data=abridged)
model3.1_dw1 <- glm(abs_dwd_score ~ treatment + post_reform + incumbent + open + state_upper + party + dist_extreme + more_extreme + calif, data=abridged)
summary(model3b.FE_dw1)
summary(model3.1_dw1 )


data2 <- read.csv("test2data_dw.csv")
abridged <- data.frame(cbind(data2$treatment, data2$INCUMBENT, data2$OPEN, data2$IDEOLOGY, data2$moderate, data2$DIST_TO_ZERO, data2$party, 
                             data2$congress, data2$state_upper, data2$abs_closer_zero, data2$y2012, data2$y2010, data2$y2008, 
                             data2$post_reform, data2$year, data2$calif, data2$wash, data2$dw_dime))
colnames(abridged) <- c("treatment", "incumbent", "open", "ideology", "moderate", "candidate_ext", 
                        "party", "congress", "state_upper", "more_extreme", "y2012", "y2010", "y2008",
                        "post_reform", "year", "calif", "wash", "dw_dime")
abridged$dwd_ext <- abs(abridged$dw_dime)
abridged$dist_extreme <- abs(data2$tw_district_ideo) # how extreme the district is
abridged$dist_ideo <- data2$tw_district_ideo
model3b.FE_dw2 <- glm(dwd_ext ~ treatment  + party +incumbent + open + state_upper + dist_extreme + more_extreme + calif, data=abridged)
model3c_dw2 <- glm(dwd_ext ~ treatment + party +incumbent + open + state_upper + dist_extreme + more_extreme + calif + factor(year), data=abridged)


data3.1 <- read.csv("final_merged_data2shor.csv")
abridged1 <- data.frame(cbind(data3.1$treatment, data3.1$in_analysis, data3.1$INCUMBENT, data3.1$OPEN, data3.1$dw_dime, data3.1$moderate, data3.1$DIST_TO_ZERO, data3.1$Party,
                              data3.1$congress, data3.1$state_upper, data3.1$abs_closer_zero, data3.1$year2012, data3.1$year2010, data3.1$year2008, data3.1$toptwo, data3.1$calif, data3.1$wash, data3.1$YEAR))
colnames(abridged1) <- c("treatment", "in_analysis", "incumbent", "open", "dw_dime", "moderate", "candidate_ext", "party", "congress", "state_upper", "more_extreme", "y2012", "y2010", "y2008", "toptwo", "calif", "wash", "year")
abridged1$dist_extreme <- abs(data3.1$tw_district_ideo) # how extreme the district is
abridged1$dist_ideo <- data3.1$tw_district_ideo
abridged1$dw_ext <- abs(data3.1$dw_dime)
model3.1FE_dw3 <- glm(dw_ext ~ treatment + party + incumbent + open + state_upper + dist_extreme + more_extreme+ calif + wash, data=abridged1)
model3.1FEb_dw3 <- glm(dw_ext ~ treatment + party + incumbent + open + state_upper + dist_extreme + more_extreme + calif + wash + factor(year), data=abridged1)

# To make Table A2
stargazer(model3b.FE_dw1, model3.1_dw1, model3b.FE_dw2, model3c_dw2, model3.1FE_dw3, model3.1FEb_dw3, out = "tableA2.tex")

## Appendix C: Generating Matched Sample from Full Dataset of Races

require(dplyr)
cf <- read.csv("cf_scores_legislators.csv")

# Pull in the TW data

uppers10 <- read.csv("ssd_2012_TW_ideology_estimates_upper.csv")
lowers10 <- read.csv("shd_2012_TW_ideology_estimates_lower.csv")
uppers00 <- read.csv("ssd_2002_TW_ideology_estimates_upper.csv")
lowers00 <- read.csv("shd_2002_TW_ideology_estimates_lower.csv")

uppers10$state_upper <- 1
lowers10$state_upper <- 0
uppers00$state_upper <- 1
lowers00$state_upper <- 0

names(uppers10)[3] <- "mrp_mean_upper10"
names(lowers10)[3] <- "mrp_mean_lower10"
names(uppers00)[4] <- "mrp_mean_upper00"
names(lowers00)[4] <- "mrp_mean_lower00"

uppers10 <- uppers10 %>% dplyr::select(abb, district, mrp_mean_upper10)
lowers10 <- lowers10 %>% dplyr::select(abb, district, mrp_mean_lower10)
uppers00 <- uppers00 %>% dplyr::select(abb, district, mrp_mean_upper00)
lowers00 <- lowers00 %>% dplyr::select(abb, district, mrp_mean_lower00)

uppers10$abb <- as.character(uppers10$abb)
uppers00$abb <- as.character(uppers00$abb)
lowers10$abb <- as.character(lowers10$abb)
lowers00$abb <- as.character(lowers00$abb)


uppers10$year <- 2010
uppers00$year <- 2000
lowers10$year <- 2010
lowers00$year <- 2000
cf$state <- as.character(cf$state)

uppers10$dist_merge <- paste(uppers10$abb, uppers10$district, sep = "-")
uppers00$dist_merge <- paste(uppers00$abb, uppers00$district, sep = "-")
lowers10$dist_merge <- paste(lowers10$abb, lowers10$district, sep = "-")
lowers00$dist_merge <- paste(lowers00$abb, lowers00$district, sep = "-")

#all_tw <- bind_rows(uppers10, uppers00, lowers10, lowers00)
#all_tw$dist_merge <- paste(all_tw$abb, all_tw$district, sep = "-")
cf$dist_merge <- paste(cf$state, cf$dist_number, sep = "-")

cf <- merge(cf, uppers10, by = "dist_merge")
cf <- merge(cf, uppers00, by = "dist_merge")
cf <- merge(cf, lowers10, by = "dist_merge")
cf <- merge(cf, lowers00, by = "dist_merge")



cf$tw_district <- ifelse(cf$cycle < 2011 & cf$state_upper == 1, cf$mrp_mean_upper00,
                         ifelse(cf$cycle > 2010 & cf$state_upper == 1, cf$mrp_mean_upper10,
                                ifelse(cf$cycle < 2011 & cf$state_upper == 0, cf$mrp_mean_lower00,
                                       ifelse(cf$cycle > 2010 & cf$state_upper == 0, cf$mrp_mean_lower10,
                                              NA))))

cf$treatment <- 0

cf <- subset(cf, !(state %in% c("CA", "WA")))
cf$dist_extreme <- abs(cf$tw_district)
cf$cand_extreme <- abs(cf$recipient.cfscore)
cf$dist_ideo <- cf$tw_district
cf$year <- cf$cycle
cf <- subset(cf, gen.elec.stat == "W")
cf_select <- cf %>% dplyr::select(treatment, incumbent, open, state_upper, dist_extreme, cand_extreme, dist_ideo, party, year)


## Now merge to matching data from Test 3

# Treated Data

abridged1 <- subset(abridged1, in_analysis == 1)
match.data <- data.frame(cbind(abridged1$treatment, abridged1$incumbent, abridged1$open, 
                               abridged1$state_upper, abridged1$dist_extreme, abridged1$candidate_ext, abridged1$dist_ideo, abridged1$party,
                               abridged1$year))
names(match.data) <- c("treatment", "incumbent", "open", "state_upper", "dist_extreme", 
                       "cand_extreme", "dist_ideo", "party", "year")

match.data <- bind_rows(match.data, cf_select)
match.data <- data.frame(na.omit(match.data))
xcov1 <- cbind(match.data$incumbent, match.data$open, match.data$state_upper, match.data$dist_extreme, match.data$party)
colnames(xcov1) <- c("incumbent", "open", "state_upper", "dist_extreme", "party")
xcov2 <- cbind(match.data$incumbent, match.data$open, match.data$state_upper, match.data$dist_extreme, match.data$party, match.data$year)
colnames(xcov2) <- c("incumbent", "open", "state_upper", "dist_extreme", "party", "year")



outcome2 <- match.data$cand_extreme
tr1 <- match.data$treatment

## ATT RESULTS (Table A3) ##

# outcome 2, treatment 1, and xcov3

set.seed(080120187)
matching <- Match(Y = outcome2, Tr = tr1, X=xcov2, ties=TRUE, estimand="ATT")
gen.matched <- GenMatch(Tr = tr1, X = xcov2, ties=TRUE)
set.seed(080120187)
gen.match2 <- Match(Y = outcome2, Tr = tr1, X=xcov2, Weight.matrix=gen.matched, estimand="ATT")
MatchBalance(tr1 ~ xcov2, match.out=gen.match2)

summary(gen.match2)


## ATC RESULTS (Table A3) ##

# outcome 2, treatment 1, and xcov2 

set.seed(080120182)
matchingC <- Match(Y = outcome2, Tr = tr1, X=xcov2, ties=TRUE, estimand="ATC")
gen.matched <- GenMatch(Tr = tr1, X = xcov2, ties=TRUE)
set.seed(080120182)
gen.match2C <- Match(Y = outcome2, Tr = tr1, X=xcov2, Weight.matrix=gen.matched, estimand="ATC")
MatchBalance(tr1 ~ xcov2, match.out=gen.match2C)

summary(gen.match2C)


sink()
